from recordclass import recordclass
import pandas as pd
import requests
from time import localtime, strftime

Ticker = recordclass('Ticker', ['futures', 'index', 'index_30m', 'spot'])

futures = {}
indices = {}
spots = {}

timeout = 5
r = requests.get("https://www.bitmex.com/api/v1/instrument/active", timeout=timeout)
for x in r.json():
  futures[x["symbol"]] = float(x["lastPrice"])
r = requests.get("https://www.bitmex.com/api/v1/instrument/indices", timeout=timeout)
for x in r.json():
  indices[x["symbol"]] = float(x["lastPrice"])
r = requests.get("https://www.binance.com/api/v1/ticker/allPrices", timeout=timeout)
for x in r.json():
  spots[x["symbol"]] = float(x["price"])

ticker_name_list = [
    Ticker('XBTM18', '.BXBT', '.BXBT30M', 'BTCUSDT'),
    Ticker('ADAM18', '.ADAXBT', '.ADAXBT30M', 'ADABTC'),
    Ticker('BCHM18', '.BCHXBT', '.BCHXBT30M', 'BCCBTC'),
    Ticker('ETHM18', '.ETHXBT', '.ETHXBT30M', 'ETHBTC'),
    Ticker('LTCM18', '.LTCXBT', '.LTCXBT30M', 'LTCBTC'),
    Ticker('XRPM18', '.XRPXBT', '.XRPXBT30M', 'XRPBTC'),
]

ticker_list = []
for ticker_name in ticker_name_list:
  ticker_list.append(
      Ticker(futures[ticker_name.futures],
             indices[ticker_name.index],
             indices[ticker_name.index_30m],
             spots[ticker_name.spot]))

df = pd.DataFrame(ticker_list, index=[x.futures for x in ticker_name_list], columns=Ticker._fields)
df["index_30m_ratio"] = df["futures"] / df["index_30m"]
df["spot_ratio"] = df["futures"] / df["spot"]

pd.set_option('display.width', 1000)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)

print(strftime("%Y-%m-%d %H:%M:%S", localtime()))
print(df)
